/**
 * 
 */
package ar.edu.unicen.exa.oop.bbquery;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.HashMap;

import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ar.edu.unicen.exa.oop.bbquery.aggregator.BadDocumentException;
import ar.edu.unicen.exa.oop.bbquery.aggregator.TableDOMExtractor;
import ar.edu.unicen.exa.oop.bbquery.forum_service.crawler.Crawler;
import ar.edu.unicen.exa.oop.bbquery.forum_service.crawler.CrawlerFactory;
import ar.edu.unicen.exa.oop.bbquery.forum_service.crawler.DOMCrawler;
import ar.edu.unicen.exa.oop.bbquery.forum_service.crawler.DefaultCrawlerFactory;
import ar.edu.unicen.exa.oop.bbquery.forum_service.crawler.UnknownForumException;
import ar.edu.unicen.exa.oop.bbquery.model.forum.ForumAuthToken;
import ar.edu.unicen.exa.oop.bbquery.model.forum.PhpBBForumAuthTokenFactory;
import ar.edu.unicen.exa.oop.bbquery.model.forum.Post;
import ar.edu.unicen.exa.oop.bbquery.persistence.DAOFactory;
import ar.edu.unicen.exa.oop.bbquery.persistence.HibernateDAOFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author nicolas
 * 
 */
public class CLI {
	public static Logger log = LoggerFactory.getLogger(CLI.class);

	/**
	 * @param args
	 * @throws PlexusContainerException
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws MalformedURLException {
		// System.setProperty("http.agent",
		// "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; Media Center PC 6.0; InfoPath.2; MS-RTC LM");

		try {
			/* picocontainer queda para después ... */
			// MutablePicoContainer pico= new DefaultPicoContainer( new
			// AnnotatedFieldInjection());

			DAOFactory hdaof = HibernateDAOFactory.getDaoFactory();

			ForumAuthToken faccess = PhpBBForumAuthTokenFactory
					.getPhpBBForumAuthTokenFactory("localhost", "nicolas",
							"ramonator", "/phpBB3");

			/*
			 * GenericDAO<ForumAuthToken, Integer> fatDAO = hdaof
			 * .createDAO(ForumAuthToken.class);
			 * 
			 * 
			 * if (fatDAO.findById(faccess) == null) fatDAO.create(faccess);
			 */

			HashMap<String, Class<? extends Crawler>> hm = new HashMap<String, Class<? extends Crawler>>();

			hm.put("phpbb", DOMCrawler.class);

			CrawlerFactory f = new DefaultCrawlerFactory(hm);

			Crawler ffetcher = f.createCrawler(faccess);
			/*
			 * ForumBuilder fbuilder = new ForumBuilderImpl(); ForumDirector
			 * fdirector = new ForumDirector(ffetcher);
			 */
			XStream str = new XStream(new DomDriver());

			TableDOMExtractor<Post, Integer> tde = (TableDOMExtractor<Post, Integer>) str
					.fromXML(new FileInputStream(
							"src/main/resources/feedAggregator.xml"));

			SAXBuilder s = new SAXBuilder("org.ccil.cowan.tagsoup.Parser");

			tde.process(s.build(new FileInputStream(
					"src/main/resources/viewforum.html")));

			/*
			 * Forum forum = fdirector.constructForum(fbuilder);
			 * 
			 * GenericDAO<DefaultForum, Integer> fdao = hdaof
			 * .createDAO(DefaultForum.class);
			 * 
			 * fdao.create((DefaultForum) forum);
			 */

			/* HibernateHelper.close(); */

			/*
			 * XStream xenc = new XStream(new DomDriver());
			 * 
			 * FileOutputStream f_out = new FileOutputStream("forum.xml");
			 * log.info("Writing the Forum"); PrintStream printstr = new
			 * PrintStream(f_out); xenc.toXML(forum, printstr);
			 * 
			 * f_out.close();
			 */
			hdaof.terminate();
		} catch (UnknownForumException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadDocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
